<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Calendar
 *
 * @author seth
 */
class EventCalendar {
    //put your code here
    var $cself ;
    //$url="?page=event&itemid=";
    var  $url="#";
    var $dateEvent  = array();

  

    public function __construct() {

    }
    function setEventData($data){
        $k=0;
        foreach ($data as $row){
             $url = $this->fxn->processSpecialURL("page", $row["content_id"], $row["page_title"], $row["direct_link"],$_GET['h']);
$eventDate = substr($row['date_created'],0,10);
            $this->dateEvent[$k]=array(
                  "url"    => $url,
                  "title" => $row['page_title'],
                  "date"  => $eventDate);
            $k++;
        }

    }

    function drawCalendar(){
        $m = @date("m");
        $y = @date("y");
        if(isset($_GET['m']))
        $m = $_GET['m'];
        if(isset($_GET['y']))
        $y = $_GET['y'];

        $d_var=getdate(mktime(0,0,0,$m,1,$y));

        echo $this->drawCalendars($d_var);
    }

    function drawCalendars($d_var){
        $start = $d_var[wday];
        if($start==0)
        $start=7;
        $month =  $d_var[mon];
        $year =  $d_var[year];
        $today = date("Y-m-d");

        //	$query_gallist = "SELECT * FROM  event where eventdate like '$year"."-"."$month"."%' ";
        //	mysql_select_db($this->['database_imaCn'],$this->['imaCn']);

        //	$rs = mysql_query($query_gallist, $this->['imaCn']) or die(mysql_error()." Error - $linkType ".$query_gallist);
        //return $rs;

        //$dateEvents = array();
    /*$k=0;
    while($row = mysql_fetch_assoc($rs)){
         $this->["dateEvent"][$k]= array(
                  "id" => $row['eventid'],
                  "title" => $row['eventtitle'],
                  "date" => $row['eventdate'],
                  "venue" => $row['venue']);
    $k++;
    }*/
        $div = $_GET['div'];
        $html="<table width='100%' class='event_head'>
            <tr>
                <td>
                        <a href=\"javascript:getData('".$this->previousMonth($month,$year)."&div=".$div."','".$div."')\">&laquo;</a>
                </td>
                <td align='center'>".$d_var[month]." - ". $d_var[year]."</td>
                <td>
                    <a href=\"javascript:getData('".$this->nextMonth($month,$year)."&div=".$div."','".$div."')\">&raquo;</a>
            </td>
            </tr>
            </table>";
        $monthdays = array(0,31,28,31,30,31,30,31,31,30,31,30,31);
        $days = array("Mon","Tue","Wed","Thu","Fri","Sat","Sun");
        $html.= "<table width='100%' cellspacing='0' class='date_allround date_event' >
                    <tr class='date_head'>";
        for($i=0;$i< count($days);$i++)
        $html.= "
                        <td>".$days[$i]."</td>";
        $html.="
                    </tr>
                    <tr>";
        for($i=0;$i<$start-1;$i++)
        $html.= "
                        <td> &nbsp; </td>";
        for($j=1;$j<=$monthdays[$month];$j++){
            if((($start+$j-2) % 7) ==0)
            $html.="
                    </tr>
                    <tr>";
            $p =$this->correctDateFormat($year,$month,$j);
            $class="";
            if($today == $p)
            $class ="today";

            $html.=$this->isEventToday($p,$j,$class);
        }
        $others = ($start+$monthdays[$month]-1)%7;
        if($others==0){
            $html.="
                    </tr>";
        }else{
            for($k=0;$k< (7-$others);$k++)
            $html.= "
                        <td>&nbsp;</td>";
            $html.="
                    </tr>";
        }
        $html.="
        </table>";
        return  $html;
    }
    function isEventToday($date,$day,$class){
        $toHtml="";
        $toHtml= "<td class='".$class."'>".$day."</td>";
        for($i=0;$i<count($this->dateEvent);$i++)
        {
            if( substr($this->dateEvent[$i]["date"],0,10) == substr($date,0,10))
            {

                if($_GET['details']== "yes")
                {
                    $toHtml="<td class='event ".$class."'>".$day."
                            <a href='".$this->dateEvent[$i]["url"]."' title='".$this->dateEvent[$i]["title"]."-".$this->dateEvent[$i]["venue"]."'>"."<br />".$this->dateEvent[$i]["title"]."-".$this->dateEvent[$i]["venue"]."";
                }else{
                    $toHtml="<td class='event ".$class."'><a href='".$this->dateEvent[$i]["url"]."' title='".$this->dateEvent[$i]["title"]."-".$this->dateEvent[$i]["venue"]."'>".$day."";
                }

                $toHtml.="</a></td>";
            }
        }
        return $toHtml;

    }

    function  correctDateFormat($y,$m,$d)
    {
        if($m<10)
        $m="0".$m;
        if($d<10)
        $d="0".$d;
        return $y."-".$m."-".$d;
    }

    function nextMonth($m,$y){
        $m+=1;
        if($m>12)
        {
            $m=1;
            $y=$y+1;
        }
        return $_SERVER['PHP_SELF']."?m=".$m."&y=".$y."&e=".$_GET['e']."&h=".$_GET['h'];
    }

    function previousMonth($m,$y)
    {

        $m-=1;
        if($m<1)
        {
            $m=12;
            $y-=1;
        }
        return $_SERVER['PHP_SELF'].'?m='.$m.'&y='.$y."&e=".$_GET['e']."&h=".$_GET['h'];
    }
    //getPageSize("http://localhost/ma/index.pp");


}
?>
